| Conditions | 10 |
| Total Lines | 78 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like vote-card.ts ➔ buildVoteCard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import {choiceSection} from './helpers/vote'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Builds the configuration form. |
||
| 50 | * |
||
| 51 | * @param {object} state - Current state of poll |
||
| 52 | * @param {object} state.author - User that submitted the poll |
||
| 53 | * @param {string} state.topic - Topic of poll |
||
| 54 | * @param {string[]} state.choices - Text of choices to display to users |
||
| 55 | * @param {object} state.votes - Map of cast votes keyed by choice index |
||
| 56 | * @param {object} state.choiceCreator - Map of cast votes keyed by choice index |
||
| 57 | * @param {boolean} state.anon - Is anonymous?(will save voter name or not) |
||
| 58 | * @param {boolean} state.optionable - Can other user add other option? |
||
| 59 | * @returns {chatV1.Schema$CardWithId} card |
||
| 60 | */ |
||
| 61 | export function buildVoteCard(state: PollState) { |
||
| 62 | const sections = []; |
||
| 63 | const stateString = JSON.stringify(state); |
||
| 64 | |||
| 65 | const votes: Array<Array<Voter>> = Object.values(state.votes!); |
||
| 66 | const totalVotes: number = votes.reduce((sum, vote) => sum + vote.length, 0); |
||
| 67 | for (let i = 0; i < state.choices.length; ++i) { |
||
| 68 | const creator = state.choiceCreator?.[i]; |
||
| 69 | const section = choiceSection(i, state, totalVotes, stateString, creator); |
||
| 70 | sections.push(section); |
||
| 71 | } |
||
| 72 | const buttons = []; |
||
| 73 | if (state.optionable) { |
||
| 74 | buttons.push({ |
||
| 75 | 'text': 'Add Option', |
||
| 76 | 'onClick': { |
||
| 77 | 'action': { |
||
| 78 | 'function': 'add_option_form', |
||
| 79 | 'interaction': 'OPEN_DIALOG', |
||
| 80 | 'parameters': [], |
||
| 81 | }, |
||
| 82 | }, |
||
| 83 | }); |
||
| 84 | } |
||
| 85 | if (state.closable) { |
||
| 86 | buttons.push( |
||
| 87 | { |
||
| 88 | 'text': 'Close Poll', |
||
| 89 | 'onClick': { |
||
| 90 | 'action': { |
||
| 91 | 'function': 'close_poll_form', |
||
| 92 | 'interaction': 'OPEN_DIALOG', |
||
| 93 | 'parameters': [], |
||
| 94 | }, |
||
| 95 | }, |
||
| 96 | }); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (buttons.length > 0) { |
||
| 100 | sections.push( |
||
| 101 | { |
||
| 102 | 'widgets': [ |
||
| 103 | { |
||
| 104 | 'buttonList': { |
||
| 105 | buttons, |
||
| 106 | }, |
||
| 107 | }, |
||
| 108 | ], |
||
| 109 | }); |
||
| 110 | } |
||
| 111 | const card: chatV1.Schema$CardWithId = { |
||
| 112 | 'cardId': 'unique-card-id', |
||
| 113 | 'card': { |
||
| 114 | sections, |
||
| 115 | }, |
||
| 116 | }; |
||
| 117 | const authorName = state.author?.displayName ?? ''; |
||
| 118 | if (state.topic.length > 40) { |
||
| 119 | const widgetHeader = sectionHeader(state.topic, authorName); |
||
| 120 | card.card!.sections = [widgetHeader, ...sections]; |
||
| 121 | } else { |
||
| 122 | card.card!.header = cardHeader(state.topic, authorName); |
||
| 123 | } |
||
| 124 | return card; |
||
| 125 | } |
||
| 126 |